<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>检查一个变量是否为空</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.empty" class="refentry">
   <div class="refnamediv">
    <h1 class="refname">empty</h1>
    <p class="verinfo">(PHP 4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">empty</span> &mdash; <span class="dc-title">检查一个变量是否为空</span></p>

   </div>
 <div class="refsect1 description" id="refsect1-function.empty-description">
  <h3 class="title">说明</h3>
     <div class="methodsynopsis dc-description">
      <span class="methodname"><strong>empty</strong></span>
       ( <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$var</code></span>
      ) : <span class="type">bool</span></div>

    <p class="para rdfs-comment">
     判断一个变量是否被认为是空的。当一个变量并不存在，或者它的值等同于<strong><code>FALSE</code></strong>，那么它会被认为不存在。如果变量不存在的话，<span class="function"><strong>empty()</strong></span>并不会产生警告。
     </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-function.empty-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">var</code></dt>

     <dd>

      <p class="para">
       待检查的变量
      </p>
      <blockquote class="note"><p><strong class="note">Note</strong>: 
       <p class="para">
        在 PHP 5.5 之前，<span class="function"><strong>empty()</strong></span> 仅支持变量；任何其他东西将会导致一个解析错误。换言之，下列代码不会生效： <strong class="command">empty(trim($name))</strong>。
        作为替代，应该使用<strong class="command">trim($name) == false</strong>.
       </p>
      </p></blockquote>
      <p class="para">
       没有警告会产生，哪怕变量并不存在。
       这意味着 <span class="function"><strong>empty()</strong></span> 本质上与 <strong class="command">!isset($var) || $var == false</strong> 等价。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.empty-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   当<code class="parameter">var</code>存在，并且是一个非空非零的值时返回 <strong><code>FALSE</code></strong> 
   否则返回 <strong><code>TRUE</code></strong>.
  </p>
  <p class="para">
   以下的东西被认为是空的：
   <ul class="simplelist">
    <li class="member"><em>&quot;&quot;</em> (空字符串)</li>
    <li class="member"><em>0</em> (作为整数的0)</li>
    <li class="member"><em>0.0</em> (作为浮点数的0)</li>
    <li class="member"><em>&quot;0&quot;</em> (作为字符串的0)</li>
    <li class="member"><strong><code>NULL</code></strong></li>
    <li class="member"><strong><code>FALSE</code></strong></li>
    <li class="member"><em>array()</em> (一个空数组)</li>
    <li class="member"><em>$var;</em> (一个声明了，但是没有值的变量)</li>
   </ul>
  </p>
 </div>

 <div class="refsect1 changelog" id="refsect1-function.empty-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>5.5.0</td>
       <td>
        <p class="para">
         <span class="function"><strong>empty()</strong></span> 现在支持表达式了，而不仅仅是变量。
        </p>
       </td>
      </tr>

      <tr>
       <td>5.4.0</td>
       <td>
        <p class="para">
         检查非数字的字符串偏移量会返回 <strong><code>TRUE</code></strong>.
        </p>
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-function.empty-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-6358">
    <p><strong>Example #1 
     一个简单的 <span class="function"><strong>empty()</strong></span> 与 <span class="function"><a href="isset.html" class="function">isset()</a></span> 的比较。
    </strong></p>  
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$var&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;Evaluates&nbsp;to&nbsp;true&nbsp;because&nbsp;$var&nbsp;is&nbsp;empty<br /></span><span style="color: #007700">if&nbsp;(empty(</span><span style="color: #0000BB">$var</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'$var&nbsp;is&nbsp;either&nbsp;0,&nbsp;empty,&nbsp;or&nbsp;not&nbsp;set&nbsp;at&nbsp;all'</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;Evaluates&nbsp;as&nbsp;true&nbsp;because&nbsp;$var&nbsp;is&nbsp;set<br /></span><span style="color: #007700">if&nbsp;(isset(</span><span style="color: #0000BB">$var</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'$var&nbsp;is&nbsp;set&nbsp;even&nbsp;though&nbsp;it&nbsp;is&nbsp;empty'</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
  </p>
  <div class="example" id="example-6359">
   <p><strong>Example #2 在字符串偏移量上使用<span class="function"><strong>empty()</strong></span></strong></p>
   <div class="example-contents"><p>
    PHP 5.4 修改了当传入的是字符串偏移量时， <span class="function"><strong>empty()</strong></span> 的行为
   </p></div>
   <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$expected_array_got_string&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'somestring'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(empty(</span><span style="color: #0000BB">$expected_array_got_string</span><span style="color: #007700">[</span><span style="color: #DD0000">'some_key'</span><span style="color: #007700">]));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(empty(</span><span style="color: #0000BB">$expected_array_got_string</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(empty(</span><span style="color: #0000BB">$expected_array_got_string</span><span style="color: #007700">[</span><span style="color: #DD0000">'0'</span><span style="color: #007700">]));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(empty(</span><span style="color: #0000BB">$expected_array_got_string</span><span style="color: #007700">[</span><span style="color: #0000BB">0.5</span><span style="color: #007700">]));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(empty(</span><span style="color: #0000BB">$expected_array_got_string</span><span style="color: #007700">[</span><span style="color: #DD0000">'0.5'</span><span style="color: #007700">]));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(empty(</span><span style="color: #0000BB">$expected_array_got_string</span><span style="color: #007700">[</span><span style="color: #DD0000">'0&nbsp;Mostel'</span><span style="color: #007700">]));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
   </div>

   <div class="example-contents"><p>以上例程在PHP 5.3中的输出：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
</pre></div>
   </div>
   <div class="example-contents"><p>以上例程在PHP 5.4中的输出：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
bool(true)
bool(false)
bool(false)
bool(false)
bool(true)
bool(true)
</pre></div>
   </div>
  </div>
 </div>

 <div class="refsect1 notes" id="refsect1-function.empty-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">因为是一个语言构造器而不是一个函数，不能被
<a href="functions.variable_functions.html" class="link">可变函数</a> 调用。
</span>
</p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    当对一个不可见的对象属性使用 <span class="function"><strong>empty()</strong></span> 时，
    <a href="language.oop5.overloading.html#object.isset" class="link">__isset()</a>
    方法如果存在的话，它将会被调用。
   </p>
  </p></blockquote>
 </div>

 <div class="refsect1 seealso" id="refsect1-function.empty-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="isset.html" class="function" rel="rdfs-seeAlso">isset()</a> - 检测变量是否已设置并且非 NULL</span></li>
    <li class="member"><a href="language.oop5.overloading.html#object.isset" class="link">__isset()</a></li>
    <li class="member"><span class="function"><a href="unset.html" class="function" rel="rdfs-seeAlso">unset()</a> - 释放给定的变量</span></li>
    <li class="member"><span class="function"><a href="array_key_exists.html" class="function" rel="rdfs-seeAlso">array_key_exists()</a> - 检查数组里是否有指定的键名或索引</span></li>
    <li class="member"><span class="function"><a href="count.html" class="function" rel="rdfs-seeAlso">count()</a> - 计算数组中的单元数目，或对象中的属性个数</span></li> 
    <li class="member"><span class="function"><a href="strlen.html" class="function" rel="rdfs-seeAlso">strlen()</a> - 获取字符串长度</span></li>
    <li class="member"><a href="types.comparisons.html" class="link">The type comparison tables</a></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>